﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Assesment.Library.Interfaces;
using Assesment.Library.DomainObject.Administration.Catalog;
using System.Data.SqlClient;
using System.Data;
using Assesment.Library.Enum;


namespace Assesment.Library.DataAccess.Administration.Catalog
{
    public partial class LevelJabatanRepo : ISearchable<LevelJabatan>
    {
        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {

                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM master.LevelJabatan {1}", column, parameters);
                            break;

                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<LevelJabatan> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<LevelJabatan> tt = new List<LevelJabatan>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat("      JobLevelID, NamaLevel, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy ");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      master.LevelJabatan ");
                    sbQuery.AppendFormat("      {0} ", parameters);
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                LevelJabatan t = new LevelJabatan();
                                t.JobLevelID = dr[LevelJabatan.Fields.JobLevelID] == DBNull.Value ? 0 : int.Parse(dr[LevelJabatan.Fields.JobLevelID].ToString());
                                t.NamaLevel = dr[LevelJabatan.Fields.NamaLevel] == DBNull.Value ? string.Empty : dr[LevelJabatan.Fields.NamaLevel].ToString();
                                t.CreatedDate = dr[LevelJabatan.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelJabatan.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[LevelJabatan.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[LevelJabatan.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[LevelJabatan.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelJabatan.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[LevelJabatan.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[LevelJabatan.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }
    }
}
